/*
 * @Author: your name
 * @Date: 2020-01-10 17:18:27
 * @LastEditTime: 2020-04-27 17:29:13
 * @LastEditors: huacong
 * @Description: In User Settings Edit
 * @FilePath: \pc\src\utils\menuTree.js
 */
import _ from 'lodash';

const getMenus = configMenus => {
  let list = [];
  configMenus.forEach(element => {
    if (element.routes && element.routes.length > 0) {
      list.push(...getMenus(element.routes));
    } else {
      list.push(element);
    }
  });
  return list;
};

export const getFirstPage = (tree, permissions) => {
  let result = 0;
  const flatConfigMenus = getMenus(tree);
  const flatConfigMenu = _.first(
    _.filter(
      flatConfigMenus,
      item =>
        _.indexOf(
          permissions,
          item.authority && item.authority.length > 0 ? item.authority[0] : 0,
        ) > -1,
    ),
  );
  // console.log({ flatConfigMenus, flatConfigMenu }, 'flatConfigMenus');
  return flatConfigMenu ? flatConfigMenu.path : 'empty';
};
